package com.zx.platform.dao.impl;

import com.zx.platform.dao.DirectoryDao;
import com.zx.platform.dao.mapper.DirectoryMapper;
import com.zx.platform.dao.model.Directory;
import com.zx.platform.dao.model.DirectoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-1
 * Time: 上午10:54
 * To change this template use File | Settings | File Templates.
 */
@Component("directoryDao")
public class DirectoryImpl implements DirectoryDao {

    @Autowired
    private DirectoryMapper directoryMapper;

    public String addDirectory(Directory directory) throws DataAccessException {
        int column = directoryMapper.insert(directory);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void updateDirectory(Directory directory) throws DataAccessException {
        int column = directoryMapper.updateByPrimaryKeySelective(directory);
        if (column != 1) {
            throw new RuntimeException("");
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<Directory> getDirectory(int pageSize, int pageBegin, DirectoryExample example) throws DataAccessException {
        return directoryMapper.getDirectory(pageSize, pageBegin, example);  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void deleteDirectory(Directory directory) throws DataAccessException {
        int column = directoryMapper.deleteByPrimaryKey(directory.getDirId());
        if (column != 1) {
            throw new RuntimeException("");
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public int countDirectory(DirectoryExample example) throws DataAccessException {
        return directoryMapper.countByExample(example);
    }

    public Directory getDirectory(DirectoryExample example) throws DataAccessException {
        List list = directoryMapper.selectByExample(example);
        if(null != list && list.size() ==1)
        {
            return (Directory) list.get(0);
        }else
        {
           return null;
        }
    }

    public List<Directory> getDirectoryList(DirectoryExample example) throws DataAccessException {
    	if(example==null){
			example= new DirectoryExample();
		}
		example.setOrderByClause("dirId");
        return directoryMapper.selectByExample(example);
    }
    public Directory getModelById(Integer dirId) throws DataAccessException {
    	Directory directory = directoryMapper.selectByPrimaryKey(dirId);
    	return directory;
    }
}
